<template>
  <div>
    <el-form :model="form" inline :rules="rules" ref="ruleFormRef">

      <el-form-item label="盘货用户:">
        <el-select v-model="form.operationName">
          <el-option :label="item.value" :value="item.value" v-for="item in store.InventoryUser" :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核用户:">
        <el-select v-model="form.approveName">
          <el-option :label="item.value" :value="item.value" v-for="item in store.AuditUser" :key="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态:" prop="reviewerIsOperation">
        <el-select v-model="form.reviewerIsOperation">
          <el-option label="全部" :value="0" />
          <el-option label="待审核" :value="1" />
          <el-option label="已审核" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="审核时间:">
        <el-date-picker v-model="times" type="datetimerange" start-placeholder="开始时间" value-format="x"
          end-placeholder="结束时间" time-format="A hh:mm:ss" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit(ruleFormRef)">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div class="table">
    <el-table ref="multipleTableRef" :data="tableData" style="width: 100%" @selection-change="handleSelectionChange"
      border>
      <el-table-column type="selection" width="55" />
      <el-table-column label="盘货用户" width="130">
        <template #default="scope">{{ scope.row.operationName }}</template>
      </el-table-column>
      <el-table-column label="审核用户" width="130">
        <template #default="scope">{{ scope.row.createBy }}</template>
      </el-table-column>
      <el-table-column label="盘货时间" width="180">
        <template #default="scope">{{ scope.row.operationApproveTime }}</template>
      </el-table-column>
      <el-table-column label="审核时间" width="180">
        <template #default="scope">{{ scope.row.reviewerApproveTime }}</template>
      </el-table-column>
      <el-table-column label="移位时间" width="180">
        <template #default="scope">{{ scope.row.reviewerApproveTime }}</template>
      </el-table-column>
      <el-table-column label="二次移位时间" width="180">
        <template #default="scope">{{ scope.row.reviewerApproveTime }}</template>
      </el-table-column>
      <el-table-column label="移位前库位" width="180">
        <template #default="scope">{{ scope.row.moveSourceId }}</template>
      </el-table-column>
      <el-table-column label="移位后库位" width="180">
        <template #default="scope">{{ scope.row.moveTargetId }}</template>
      </el-table-column>
      <el-table-column label="移动数量" width="180">
        <template #default="scope">{{ scope.row.operationNum }}</template>
      </el-table-column>
      <el-table-column label="盘货结果">
        <template #default="scope">
          <el-button link type="primary" size="small" @click.prevent="disposeBtn(scope.row)"
            :disabled="!scope.row.operationIsMove == 0">
            {{ scope.row.operationIsMove == 0 ? '待审核' : '已审核' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <el-button type="primary" @click="everyDispose">选中批量处理</el-button>
  <div class="page">
    <el-pagination v-model:current-page="currentPage4" v-model:page-size="pageSize4" :page-sizes="[10, 50, 100]"
      layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" />
  </div>
</template>

<script setup>
import { ref, onBeforeMount, reactive } from 'vue'
import { useAlertsStore } from '@/store/index'
import { inventoryMoveOrderlist, handleSubMoveOrder } from '@/api/index'
import { ElMessage } from 'element-plus'
const store = useAlertsStore()
onBeforeMount(() => {
  store.searchInventoryMoveOrderDicApi()
})
const times = ref('')
// do not use same name with ref
const form = ref({
  pageSize: 10,
  pageNum: 1,
  operationIsMove: 1
})
const tableData = ref([])
const ruleFormRef = ref()
const rules = reactive({
  reviewerIsOperation: [
    { required: true, message: '必填', trigger: 'change' }
  ]

})

const onSubmit = async (formEl) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      getList()
    } else {
      console.log('error submit!', fields)
    }
  })
}
const getList = () => {
  if (times.value) {
    form.value.printTimeStart = times.value[0]
    form.value.printTimeEnd = times.value[1]
  } else {
    delete form.value.printTimeStart
    delete form.value.printTimeEnd
  }
  inventoryMoveOrderlist(form.value).then(res => {
    const { data: { data } } = res
    total.value = data.total
    tableData.value = data.list
  })
}
// 分页
const currentPage4 = ref(1)
const pageSize4 = ref(10)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  form.value.pageSize = val
  getList()
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  form.value.pageNum = val
  getList()
}

// table
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 处理
const disposeBtn = (row) => {
  handleSubMoveOrder({
    status: 2,
    inventoryMoveOrderIds: [row.id]
  }).then(res => {
    console.log(res)
    ElMessage.success('操作成功')
    getList()
  })
}
const everyDispose = () => {
  const ids = []
  if (multipleSelection.value.length > 0) {
    multipleSelection.value.forEach(e => {
      ids.push(e.id)
    })
    handleSubMoveOrder({
      status: 2,
      inventoryMoveOrderIds: ids
    }).then(res => {
      console.log(res)
      ElMessage.success('操作成功')
      getList()
    })
  } else {
    ElMessage.error('选择数据')
  }
}
</script>

<style lang='less' scoped></style>
